package install.ren.exercise.test.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientAssignConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author jonny
 * Created on 2022/4/16
 */
public class TokenFlowRunnable implements Runnable {

    public static void init() {
        System.setProperty("csp.sentinel.dashboard.server", "127.0.0.1:8080");
        System.setProperty("project.name", "exercise");
        // 指定当前身份为 Token Client
        ClusterStateManager.applyState(ClusterStateManager.CLUSTER_CLIENT);
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("HelloWorld");
        // set limit qps to 20
        rule.setCount(5);
        rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
        rule.setLimitApp("demo1");
        rule.setClusterMode(true);
        final ClusterFlowConfig clusterFlowConfig = new ClusterFlowConfig();
        clusterFlowConfig.setStrategy(ClusterRuleConstant.FLOW_CLUSTER_STRATEGY_NORMAL);
        clusterFlowConfig.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
        clusterFlowConfig.setFallbackToLocalWhenFail(true);
        clusterFlowConfig.setFlowId(1024L);
        rule.setClusterConfig(clusterFlowConfig);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);

        ClusterClientConfig clusterClientConfig = new ClusterClientConfig();
        clusterClientConfig.setRequestTimeout(1100);
        ClusterClientConfigManager.applyNewConfig(clusterClientConfig);

        ClusterClientAssignConfig clusterClientAssignConfig =
                new ClusterClientAssignConfig("192.168.1.7", 11111);
        ClusterClientConfigManager.applyNewAssignConfig(clusterClientAssignConfig);
    }

    @Override
    public void run() {
        Long startTime = System.currentTimeMillis();
        ContextUtil.enter("mykey", "demo1");
        try (Entry entry = SphU.entry("HelloWorld")) {
            // Your business logic here.
            System.out.println("hello world");
        } catch (Exception e) {
            // Handle rejected request.
            e.printStackTrace();
        } finally {
            long t = System.currentTimeMillis() - startTime;
            if (t>10){
                System.out.println("cost:" + t);
            }
        }

// try-with-resources auto exit
    }

    public static void main(String[] args) throws InterruptedException {
        init();
        TokenFlowRunnable tokenFlowRunnable = new TokenFlowRunnable();
        for (; ; ) {
            for (int i = 0; i < 10; i++) {
                new Thread(tokenFlowRunnable).start();
            }
            System.out.println("next");
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
